* Pretrends and crossectional comparison tests for NGO/non-NGO villages in UK

********************************************************************************

* (I) Pre-trends using Census 2001 and Census 2011 variables (Appendix Table D3)

use "${data}/uttarakhand_census_2001_2011_chirag.dta", clear

// Variables
global diffvars Total_HH Total_Pop Total_Pop_F Total_Pop_M Total_Pop_SCST ///
	Prim_School Other_School PHC_No CHW_strength ///
	Tap Tubewell Bus Elec_Agr Elec_Dom Paved_Road Post ///
	Irrig_Area Unirrig_Area
fsum $diffvars
bysort census: fsum $diffvars

// "Post" indicator
gen census2011 = (census == 2011)

// Pre-trends regressions
tempfile t
local replace replace
foreach var of varlist $diffvars {
	qui reghdfe `var' i.census2011##i.chirag_village, absorb(dtc2001) vce(robust) 
	qui regsave 1.census2011#1.chirag_village using "`t'", detail(all) addlabel(reg,`var') `replace'
    local replace append
	}

// Generate table
use `t', clear
keep depvar coef stderr r2 N 
order depvar coef stderr r2 N 
gen sortorder = _n * -1
sort sortorder
drop sortorder
local max = _N
foreach var of varlist coef stderr r2 {
	format `var' %5.2f
	forval i = 1/`max' {
		qui sum `var' if _n == `i'
		autofmt, input(`r(mean)') dec(2) // 2 significant figures
		replace `var' = `r(output1)' if _n == `i'
	}	
}

// Full dependent variable names
replace depvar = "Number of households" if _n == 1
replace depvar = "Total population" if _n == 2
replace depvar = "Total population (females)" if _n == 3
replace depvar = "Total population (males)" if _n == 4
replace depvar = "Total population (Scheduled Caste/Scheduled Tribe)" if _n == 5
replace depvar = "Number of primary schools" if _n == 6
replace depvar = "Number of other educational facilities" if _n == 7
replace depvar = "Number of primary health centers" if _n == 8
replace depvar = "Number of community health workers" if _n == 9
replace depvar = "\(\mathbbm{1} \left( \text{Tap water} \right)\)" if _n == 10
replace depvar = "\(\mathbbm{1} \left( \text{Tubewell} \right)\)" if _n == 11
replace depvar = "\(\mathbbm{1} \left( \text{Bus services} \right)\)" if _n == 12 
replace depvar = "\(\mathbbm{1} \left( \text{Electricity for agricultural use} \right)\)" if _n == 13
replace depvar = "\(\mathbbm{1} \left( \text{Electricity for domestic use} \right)\)" if _n == 14
replace depvar = "\(\mathbbm{1} \left( \text{Approach to village: paved road} \right)\)" if _n == 15 
replace depvar = "\(\mathbbm{1} \left( \text{Post office} \right)\)" if _n == 16
replace depvar = "Total irrigated land area (hectares)" if _n == 17
replace depvar = "Total unirrigated land area (hectares)" if _n == 18 

// Save table
texsave using "${results}apptable_pretrends.tex", replace ///
	frag nonames nofix

********************************************************************************

* (II) Cross-sectional comparisons using Census 2011 variables (that do not appear to exist on Census 2001 at the village-level) (Appendix Table D2)

use "${data}/uttarakhand_census_2011_detailed_chirag.dta", clear

// District dummy
destring district_code, gen(dist_code)
tab dist_code, gen(dist_dummy)

// Variable labels for balance table
lab var hh_rooms_0 "Dwelling rooms: 0" 
lab var hh_rooms_1 "Dwelling rooms: 1"
lab var hh_rooms_2 "Dwelling rooms: 2"
lab var hh_rooms_3 "Dwelling rooms: 3"
lab var hh_rooms_4 "Dwelling rooms: 4"
lab var hh_rooms_5 "Dwelling rooms: 5"
lab var hh_rooms_6 "Dwelling rooms: \(\geqslant 6\)"
lab var hh_size_1 "Household size: 1"
lab var hh_size_2 "Household size: 2"
lab var hh_size_3 "Household size: 3"
lab var hh_size_4 "Household size: 4"
lab var hh_size_5 "Household size: 5"
lab var hh_size_68 "Household size: 6--8"
lab var hh_size_9plus "Household size: \(\geqslant 9\)"
lab var water_tap_treated "Tap water from treated source (%)"
lab var water_tap_untreated "Water: Untreated tap"
lab var water_well_covered "Water: Covered well"
lab var water_well_uncovered "Water: Uncovered well"
lab var water_handpump "Water: Handpump"
lab var water_tubewell "Water: Tubewell"
lab var water_spring "Water: Spring"
lab var water_river_canal "Water: River/canal"
lab var water_tank_pond_lake "Water: Tank/pond/lake"
lab var water_other "Water: Other"
lab var light_electricity "Main lighting source: Electricity (%)"
lab var light_kerosene "Main lighting source: Kerosene (%)"
lab var light_solar "Light: Solar"
lab var light_other_oil "Light: Other oil"
lab var light_other "Light: Other"
lab var light_none "Light: None"
lab var latrine_onsite_percent "Latrine: Onsite"
lab var cookingfuel_wood "Fuel used for cooking: Firewood"
lab var cookingfuel_cropresidue "Cooking fuel: Crop residue"
lab var cookingfuel_dung "Cooking fuel: Dung"
lab var cookingfuel_coal "Cooking fuel: Coal"
lab var cookingfuel_kerosene "Cooking fuel: Kerosene"
lab var cookingfuel_LPG "Fuel used for cooking: LPG"
lab var cookingfuel_electricity "Fuel used for cooking: Electricity"
lab var cookingfuel_biogas "Cooking fuel: Biogas"
lab var cookingfuel_other "Cooking fuel: Other"
lab var cookingfuel_none "Cooking fuel: None"
lab var banking "Number of household availing banking services"
lab var asset_radio "Asset ownership: Radio (%)"
lab var asset_television "Asset ownership: Television (%)"
lab var asset_computer_internet "Asset: Computer (with internet)"
lab var asset_computer_nointernet "Asset: Computer (without internet)"
lab var asset_tel_landline "Asset: Telephone (landline)"
lab var asset_tel_mobile "Asset ownership: Mobile phone (%)"
lab var asset_tel_landline_mobile "Asset: Telephone (landline and mobile)"
lab var asset_bicycle "Asset ownership: Bicycle (%)"
lab var asset_scooter "Asset ownership: Scooter (%)"
lab var asset_car "Asset ownership: Car (%)"

// Generate household size average
preserve
	keep hh_size_* plcn2001
	reshape long hh_size_, i(plcn2001) j(number_of_hh_members) string
	replace number_of_hh_members = "6" if number_of_hh_members == "68"
	replace number_of_hh_members = "9" if number_of_hh_members == "9plus"
	destring number_of_hh_members, replace
	gen average_hh_size = (number_of_hh_members * hh_size_) / 100
	collapse (sum) average_hh_size, by(plcn2001)
	lab var average_hh_size "Mean household size"
	tempfile average_hh_size
	save `average_hh_size'
restore

// Generate dwelling rooms average
preserve
	keep hh_rooms_* plcn2001
	reshape long hh_rooms_, i(plcn2001) j(number_of_rooms)
	gen average_rooms = (number_of_rooms * hh_rooms_) / 100
	collapse (sum) average_rooms, by(plcn2001)
	lab var average_rooms "Number of rooms in mean dwelling"
	tempfile average_rooms
	save `average_rooms'
restore

// Merge average household size and average rooms back into data
merge 1:1 plcn2001 using `average_hh_size', gen(_merge_hh_size)
merge 1:1 plcn2001 using `average_rooms', gen(_merge_hh_rooms)

// List of balance variables
global balance_vars ///
	average_hh_size ///
	average_rooms ///
	water_tap_treated ///
	light_electricity ///
	light_kerosene ///
	cookingfuel_wood ///
	cookingfuel_LPG ///
	cookingfuel_electricity ///
	banking ///
	asset_radio ///
	asset_television ///
	asset_tel_mobile ///
	asset_bicycle ///
	asset_scooter ///
	asset_car

// Balance table - main shell
preserve

	iebaltab $balance_vars, grpvar(chirag_village) stdev normdiff starsnoadd ///
		order(1 0) format(%9.2f) tblnonote ///
		grplabels("1 NGO villages @ 0 Non-NGO villages") ///
		rowvarlabels ///
		vce(robust) ///
		fixedeffect(dist_code) ///
		onerow ///
		browse

		// Variable number for merging with MHT adjusted p-values below
		gen variable_number = _n/2 - 1
		replace variable_number = . if variable_number < 1
		replace variable_number = . if mod(variable_number,1) > 0
		replace variable_number = . if mi(v4)

		tempfile balance_table
		save `balance_table'

restore

// MHT adjustment
wyoung $balance_vars, ///
	cmd(regress OUTCOMEVAR chirag_village dist_dummy2, robust) ///
	familyp(chirag_village) ///
	bootstraps(10000) seed(20201228)
matrix list r(table)
matselrc r(table) pwyoung, c(4) // save adjusted p-values

// Add adjusted p-values back
use `balance_table', clear
levelsof variable_number, local(varnum)
gen wyoung_pvalue = .
foreach i of local varnum {
	replace wyoung_pvalue = pwyoung[`i',1] if variable_number == `i'
}
drop variable_number

// Add stars to normalized difference based on adjusted p-value
gen stars = "*" if wyoung_pvalue < 0.10 & wyoung_pvalue >= 0.05 & !mi(wyoung_pvalue)
replace stars = "**" if wyoung_pvalue < 0.05 & wyoung_pvalue >= 0.01 & !mi(wyoung_pvalue)
replace stars = "***" if wyoung_pvalue < 0.01 & !mi(wyoung_pvalue)
replace v4 = v4 + stars

// Replace with minus signs for LaTeX
foreach var of varlist v4 v5 {
	replace `var' = subinstr(`var', "-", "\(-\)",.) if _n >= 3
}

// Save balance table
save "${output}apptable_census2011_balance.dta", replace
drop stars wyoung_pvalue

// Format and save LaTeX table
drop if _n < 4 // drop table headers
texsave using "${results}apptable_census2011_balance.tex", ///
	frag replace nonames nofix

**************************

* END 
